package com.winshare.saleAssistant.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.winshare.saleAssistant.model.School;
import com.winshare.saleAssistant.service.ISchoolService;
import com.winshare.saleAssistant.util.LogicResponse;
import com.winshare.saleAssistant.util.PageFactory;
import com.winshare.saleAssistant.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 学校信息
 */
@Controller
@RequestMapping("/sys/school")
public class SchoolController {
    @Autowired
    private ISchoolService schoolService;


    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(Boolean checkbox, String province, String city, String areaName, String condition) {
        Page<School> page = new PageFactory<School>().defaultPage();
        if(checkbox == null){
            checkbox=false;
        }
        List<School> schools = schoolService.selectNumber(page, checkbox, province, city, areaName, condition);
        page.setRecords(schools).getRecords();
        return LogicResponse.success(page);
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(School school) {
        System.out.println(school.toString());
        //全部的数据
        //学校编号唯一

         if(!ToolUtil.isEmpty(schoolService.getOne(Wrappers.<School>lambdaQuery().eq(School::getSchoolCode,school.getSchoolCode())))) {
            return LogicResponse.failure("编号重复");
        }else if(!ToolUtil.isEmpty(schoolService.getOne(Wrappers.<School>lambdaQuery().eq(School::getSchoolName,school.getSchoolName()).eq(School::getAreaName,school.getAreaName())))){
             return LogicResponse.failure("学校名字重复");
        }else if(!ToolUtil.isEmpty(schoolService.getOne(Wrappers.<School>lambdaQuery().eq(School::getSchoolFullname,school.getSchoolFullname()).eq(School::getAreaName,school.getAreaName())))){
             return LogicResponse.failure("学校名字重复");
        }
        schoolService.save(school);
        return LogicResponse.success("success");
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer schoolId) {
        schoolService.removeById(schoolId);
        return LogicResponse.success("success");
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(School school) {
        System.out.println(school.toString());
        schoolService.updateById(school);
        return LogicResponse.success("success");
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{schoolId}")
    @ResponseBody
    public Object detail(@PathVariable("schoolId") Integer schoolId) {
        return schoolService.getById(schoolId);
    }
}
